<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/4/17
 * Time: 15:41
 */

require_once('config.php');
require_once('mysqlii.php');
require_once('class.smtp.php');
require_once('class.phpmailer.php');


$db = MySqlii::getInstance();

$timeStart = strtotime(date('Y-m-d 00:00:00'));
$timeStop  = strtotime(date('Y-m-d 23:59:59'));

$sql = "SELECT count(id) as num FROM reals_leshi WHERE ctime BETWEEN $timeStart AND $timeStop";

$res = $db->fetch_row($db->query($sql));

$totleNum = $res[0];
//$totleNum = 6998;

if($totleNum >= 2000 && $totleNum < 3000) {
    pushMail($totleNum, '2k3k', 2000);
}

if($totleNum >= 3000 && $totleNum < 4000) {
    pushMail($totleNum, '3k4k', 3000);
}

if($totleNum >= 4000 && $totleNum < 5000) {
    pushMail($totleNum, '4k5k', 4000);
}

if($totleNum >= 5000 && $totleNum < 6000) {
    pushMail($totleNum, '5k6k', 5000);
}

if($totleNum >= 6000 && $totleNum < 7000) {
    pushMail($totleNum, '6k7k', 6000);
}

if($totleNum >= 7000 && $totleNum < 8000) {
    pushMail($totleNum, '7k8k', 7000);
}

if($totleNum >= 8000 && $totleNum < 9000) {
    pushMail($totleNum, '8k9k', 8000);
}

if($totleNum >= 9000 && $totleNum < 10000) {
    pushMail($totleNum, '9k0k', 9000);
}

if($totleNum >= 10000) {
    pushMail($totleNum, '0k0k', 10000);
}



function pushMail($num, $key, $val)
{
    $db = MySqlii::getInstance();
    $date = date('Ymd');
    $sql = "SELECT value FROM reals_leshi_mail WHERE name = '{$key}' AND date = $date";
    $res = $db->fetch_row($db->query($sql));
    if(empty($res)) {
        $inDsql = "INSERT INTO reals_leshi_mail (name, value, date) VALUES ('{$key}', 1, $date)";
        $db->query($inDsql);
    } else if($res[0] < 3) {
        $upSql = "UPDATE reals_leshi_mail SET value = value+1 WHERE name = '{$key}' AND date = $date";
        $db->query($upSql);
    } else {
        return;
    }

    $contents = "警告!!!!\n
                乐视App下载量为$num,超过临界值 ： $val 请悉知";

    $sendUsers = array('308736929@qq.com', 'lijia@iiapple.com', '530972476@qq.com', '656297731@qq.com');
    //$sendUsers = array('656297731@qq.com');
    foreach($sendUsers as $userName){
        sendMail($userName, '乐视下载超量预警！', $contents);
    }
}



function sendMail ($mailto, $subject,  $content) {
    $mail = new PHPMailer(); //ʵ��һ��phpmailer����
    $mail->CharSet = "UTF-8";//ָ������
    $mail->Encoding = "base64";
    $mail->Port = '25';
    $mail->IsSMTP();
    $mail->Host = '124.202.152.216';
    $mail->SMTPAuth = true;
    $mail->Username = 'cs@ucenter.iiapple.com';
    $mail->Password = 'kXAkCzpws2XcpWYX';
    $mail->From = 'cs@ucenter.iiapple.com';
    $mail->FromName = 'I苹果用户中心';
    $mail->AddAddress($mailto);
    $mail->WordWrap = 500;
    $mail->IsHTML(true);
    $mail->Subject = $subject;
    $mail->Body = $content;
    //$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
    $mail->Send();
}